package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.ItemDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class ItemView {
    private InputText txtEstado;
    private InputText txtNombre;
    private InputText txtCodigoProBack_ProductBacklog;
    private InputText txtCodigoItem;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ItemDTO> data;
    private ItemDTO selectedItem;

    public ItemView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ItemDTO itemDTO = (ItemDTO) e.getObject();

            if (txtEstado == null) {
                txtEstado = new InputText();
            }

            txtEstado.setValue(itemDTO.getEstado());

            if (txtNombre == null) {
                txtNombre = new InputText();
            }

            txtNombre.setValue(itemDTO.getNombre());

            if (txtCodigoProBack_ProductBacklog == null) {
                txtCodigoProBack_ProductBacklog = new InputText();
            }

            txtCodigoProBack_ProductBacklog.setValue(itemDTO.getCodigoProBack_ProductBacklog());

            if (txtCodigoItem == null) {
                txtCodigoItem = new InputText();
            }

            txtCodigoItem.setValue(itemDTO.getCodigoItem());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtEstado != null) {
            txtEstado.setValue(null);
            txtEstado.setDisabled(true);
        }

        if (txtNombre != null) {
            txtNombre.setValue(null);
            txtNombre.setDisabled(true);
        }

        if (txtCodigoProBack_ProductBacklog != null) {
            txtCodigoProBack_ProductBacklog.setValue(null);
            txtCodigoProBack_ProductBacklog.setDisabled(true);
        }

        if (txtCodigoItem != null) {
            txtCodigoItem.setValue(null);
            txtCodigoItem.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Item entity = null;

        try {
            String codigoItem = new String(txtCodigoItem.getValue().toString());
            entity = BusinessDelegatorView.getItem(codigoItem);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtEstado.setDisabled(false);
            txtNombre.setDisabled(false);
            txtCodigoProBack_ProductBacklog.setDisabled(false);
            txtCodigoItem.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtEstado.setValue(entity.getEstado());
            txtEstado.setDisabled(false);
            txtNombre.setValue(entity.getNombre());
            txtNombre.setDisabled(false);
            txtCodigoProBack_ProductBacklog.setValue(entity.getProductBacklog()
                                                           .getCodigoProBack());
            txtCodigoProBack_ProductBacklog.setDisabled(false);
            txtCodigoItem.setValue(entity.getCodigoItem());
            txtCodigoItem.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveItem(FacesUtils.checkString(txtCodigoItem),
                FacesUtils.checkString(txtEstado),
                FacesUtils.checkString(txtNombre),
                FacesUtils.checkString(txtCodigoProBack_ProductBacklog));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteItem(FacesUtils.checkString(
                    txtCodigoItem));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateItem(FacesUtils.checkString(
                    txtCodigoItem), FacesUtils.checkString(txtEstado),
                FacesUtils.checkString(txtNombre),
                FacesUtils.checkString(txtCodigoProBack_ProductBacklog));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoItem == null) {
                txtCodigoItem = new InputText();
            }

            txtCodigoItem.setValue(selectedItem.getCodigoItem());

            BusinessDelegatorView.deleteItem(FacesUtils.checkString(
                    txtCodigoItem));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedItem);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoItem, String estado,
        String nombre, String codigoProBack_ProductBacklog)
        throws Exception {
        try {
            BusinessDelegatorView.updateItem(codigoItem, estado, nombre,
                codigoProBack_ProductBacklog);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ItemView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtEstado() {
        return txtEstado;
    }

    public void setTxtEstado(InputText txtEstado) {
        this.txtEstado = txtEstado;
    }

    public InputText getTxtNombre() {
        return txtNombre;
    }

    public void setTxtNombre(InputText txtNombre) {
        this.txtNombre = txtNombre;
    }

    public InputText getTxtCodigoProBack_ProductBacklog() {
        return txtCodigoProBack_ProductBacklog;
    }

    public void setTxtCodigoProBack_ProductBacklog(
        InputText txtCodigoProBack_ProductBacklog) {
        this.txtCodigoProBack_ProductBacklog = txtCodigoProBack_ProductBacklog;
    }

    public InputText getTxtCodigoItem() {
        return txtCodigoItem;
    }

    public void setTxtCodigoItem(InputText txtCodigoItem) {
        this.txtCodigoItem = txtCodigoItem;
    }

    public List<ItemDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataItem();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ItemDTO> itemDTO) {
        this.data = itemDTO;
    }

    public ItemDTO getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(ItemDTO item) {
        this.selectedItem = item;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
